﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;

using ZhimeiSoft.Framework.Common.Enums;
namespace ZhimeiSoft.Framework.Client.Base
{
    public partial class BaseUserControl : UserControl
    {
        public ControlClassModeEnum ShowViewState { get; set; }

        protected ClientAppBase app;
        public BaseUserControl()
        {
            InitializeComponent();
        }

        public BaseUserControl(ClientAppBase _app)
        {
            InitializeComponent();
            this.app = _app;
        }
        /// <summary>
        /// 用来提示输入错误的控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="error"></param>
        protected void SetErrorControl(Control control, string error)
        {
            ErrorProvider errorPdr = new ErrorProvider();
            errorPdr.SetError(control, error);
        }
        /// <summary>
        /// 按回车进入下一个控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void keyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.Equals((char)Keys.Enter))
            {
                int iTableIndex = ((System.Windows.Forms.Control)sender).TabIndex + 1;
                foreach (System.Windows.Forms.Control ctrl in this.Controls)
                {
                    if (ctrl.Controls.Count > 0)
                    {
                        setControlFocus(iTableIndex, ctrl);
                    }
                }

            }
        }

        private void setControlFocus(int iNextTableIndex, System.Windows.Forms.Control currControl)
        {
            if (currControl.TabIndex == iNextTableIndex)
            {
                currControl.Focus();
                return;
            }
            else
            {
                foreach (System.Windows.Forms.Control ctrl in currControl.Controls)
                {
                    setControlFocus(iNextTableIndex, ctrl);
                }
            }
        }

        private void BaseUserControl_Load(object sender, EventArgs e)
        {
            InitGridView(this);
        }

        /// <summary>
        /// 初始化GGRID
        /// </summary>
        /// <param name="control"></param>
        protected void InitGridView(Control controls)
        {
            foreach (Control aControl in controls.Controls)
            {
                if (aControl.GetType().FullName.Equals("DevComponents.DotNetBar.TabControl"))
                {
                    //DevComponents.DotNetBar.TabControl tab = aControl as DevComponents.DotNetBar.TabControl;
                    //tab.Style = DevComponents.DotNetBar.eTabStripStyle.Office2007Document;
                }

                if (aControl is DevExpress.XtraGrid.GridControl)
                {
                    DevExpress.XtraGrid.GridControl gridControl = aControl as DevExpress.XtraGrid.GridControl;
                    if (gridControl == null)
                        continue;

                    if (((DevExpress.XtraGrid.Views.Base.BaseView)(gridControl.MainView)).GetType().ToString() == "DevExpress.XtraGrid.Views.Card.CardView")
                        continue;

                    //标题栏居中
                    gridControl.Styles.AddReplace("HeaderPanel", new DevExpress.Utils.ViewStyleEx("HeaderPanel", "Grid", "", true, false, false, DevExpress.Utils.HorzAlignment.Center, DevExpress.Utils.VertAlignment.Center, null, System.Drawing.SystemColors.Control, System.Drawing.SystemColors.ControlText, System.Drawing.Color.Empty, System.Drawing.Drawing2D.LinearGradientMode.Horizontal));

                    //焦点行反色高亮显示
                    gridControl.Styles.AddReplace("FocusedRow", new DevExpress.Utils.ViewStyleEx("FocusedRow", "Grid", new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))), ((DevExpress.Utils.StyleOptions)((((((DevExpress.Utils.StyleOptions.StyleEnabled | DevExpress.Utils.StyleOptions.UseBackColor)
                        | DevExpress.Utils.StyleOptions.UseDrawFocusRect)
                        | DevExpress.Utils.StyleOptions.UseFont)
                        | DevExpress.Utils.StyleOptions.UseForeColor)
                        | DevExpress.Utils.StyleOptions.UseImage))), System.Drawing.Color.YellowGreen, System.Drawing.SystemColors.ActiveCaptionText, System.Drawing.Color.YellowGreen, System.Drawing.Drawing2D.LinearGradientMode.Horizontal));

                    if (gridControl.MainView.GetType().FullName.ToUpper().Equals("DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView".ToUpper()))
                    {
                        
                        //居中
                        DevExpress.XtraGrid.Views.BandedGrid.BandedGridViewStylesInfo styleInfo = (BandedGridViewStylesInfo)(((AdvBandedGridView)gridControl.MainView).ViewStylesInfo);
                        styleInfo.BandPanel.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    }
                    else
                    {

                        DevExpress.XtraGrid.Views.Grid.GridView gridView = gridControl.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
                        gridView.OptionsCustomization.AllowFilter = false;
                        //gridView.OptionsCustomization.AllowGroup = true;
                        //gridView.OptionsCustomization.AllowSort = true;
                        gridView.OptionsCustomization.AllowRowSizing = false;

                        //屏蔽右键菜单
                        gridView.OptionsMenu.EnableColumnMenu = false;
                        gridView.OptionsMenu.EnableFooterMenu = false;
                        gridView.OptionsMenu.EnableGroupPanelMenu = false;
                        //屏蔽界面的一些信息
                        gridView.OptionsView.ShowDetailButtons = false;
                        gridView.OptionsView.ShowFilterPanel = false;
                        //gridView.OptionsView.ShowGroupPanel = true;
                        gridView.OptionsView.ShowIndicator = false;
                        gridView.OptionsHint.ShowCellHints = false;
                        gridView.OptionsHint.ShowColumnHeaderHints = false;

                        if (gridView.OptionsView.ShowGroupPanel == true)
                        {
                            gridView.GroupPanelText = "将下面的列拖放到这里，可以进行分组";
                            gridView.ViewStylesInfo.GroupPanel.BackColor = Color.FromArgb(169, 206, 254);
                            gridView.ViewStylesInfo.GroupPanel.BackColor2 = Color.FromArgb(169, 206, 254);
                        }
                    }
                }
                if (aControl.Controls.Count > 0)
                    InitGridView(aControl);
            }
        }
    
    }
}
